#include "header.h"
//对标准输入进行处理
/*
   int analysis(char *str){
   client_request_t client;
   client.file_path=str;
   set_struct(client);

   return 0;
   }
   */
//对路径进行处理
int set_struct(char *input,client_request_t* client){
    char *p=input;
    char buffer[1024];

    //将标准输入拆分后存至结构体内 
    strcpy(buffer,p);
    client->cmdnum=switch_to_num(strtok(buffer," "));
    client->file_path=strtok(NULL," /");

    //对命令，路径的合理性进行判断
    /*
       int ret_judge=legality_judge(client);
       if(ret_judge==-1){
       printf("路径格式输入错误\n");
       return -1;
       }
       */
    //文件上传下载时设置偏移量
    /*
    //将当前路径压栈处理
    Pathstack addr;
    initStack(*addr);
    //获取当前文件名
    while(!strtok(p,"/")){    
    addr.path=push(addr,strtok(p,"/"));
    }
    pop(addr);

    }else{
    client.offset=0;
    }

    }
    */
    //设置文件偏移量
    switch(client->cmdnum){
    case PUTS:
    case GETS:
        offset_file(client);
        break;
    }
}
//将命令转换为对应的数字
int switch_to_num(char* cmd) {  
    if (strcmp(cmd, "login") == 0) {  
        return LOGIN;  
    } else if (strcmp(cmd, "cd") == 0) {  
        return CD;  
    } else if (strcmp(cmd, "ls") == 0) {  
        return LS;  
    } else if (strcmp(cmd, "puts") == 0) {  
        return PUTS;  
    } else if (strcmp(cmd, "gets") == 0) {  
        return GETS;  
    } else if (strcmp(cmd, "remove") == 0) {  
        return REMOVE;  
    } else if (strcmp(cmd, "pwd") == 0) {  
        return PWD;  
    } else {
        //对输入指令的合理性进行判断
        printf("cmd error\n");  
        return -1;
    }  
}
int offset_file(client_request_t *client){
    char *p=client->file_path;
    char *file_name;
    if(!strchr(p,'/')){
        file_name=p+1;
    }else{
        file_name=p;
    }

    int file_fd=open(file_name,O_RDONLY,0666);
    struct stat buf;
    fstat(file_fd,&buf);
    client->offset = buf.st_size;
}

//对命令，路径的合法性进行判断
int legality_judge(client_request_t* client){
    //输入路径不合法
    //当出现连续‘/’时，输入错误
    int count=0
        while(*client->file_path=='/'){
            p++;
            if(*client->file_path=='0'){
                continue;
            }else if(*clinent->fiLe_path=='/'){
                return -1;
            }

        }
    switch(client->cmdnum){
    case 0:
        if(client->file_path==NULL){
            printf("login error!");
            return 0;
        }
        break;
    case 1:
        break;
    case 2:
        break;
    case 3:
        if(client->file_path==NULL){
            printf("puts error!");
            return 0;
        }
        break;
    case  4:
        if(client->file_path==NULL){
            printf("gets error!");
            return 0;
        }
        break;
    case 5:
        break;
    case 6:
        break;
    }    
    return 0;
}

